--#include "data\config\fuben\KongFuMasterConfig.lua" once			--练功师


KONGFUMASTER_FUBEN_ALLHURT 		= 1				--玩家在此副本的全部伤害
KONGFUMASTER_FUBEN_ATTACKNUM 	= 2				--玩家在此副本的攻击次数
KONGFUMASTER_FUBEN_MASTERIDX	= 3				--副本的MASTERIDX
KONGFUMASTER_FUBEN_SCENEID		= 4				--副本的场景ID

--[[
注册一些事件
]]
function ResisterKongFuMasterProc()
	--print("ResisterKongFuMasterProc...")
	local fubenId = KongFuMasterConfig[1].fubenId
	local sceneId = KongFuMasterConfig[1].sceneId
	SetNeedReloginFuben( fubenId, GlobalLuaConfig.reloginFuben.kongFuMaster, GetElemsForKongFuMasterBoss )  --	该副本是需要掉线恢复的
	SceneHandExitDispatcher.register( sceneId, OnCommonSceneHandExit )							--注册手工离开场景、副本后触发的事件
	FubenTimeoutDispatcher.register( fubenId, OnSingleCommonFubenTimeout )						--注册单人场景、副本时间到后触发的事件
	SceneLogoutDispatcher.register( sceneId, OnCommonFubenLogout )								--注册场景、副本中下线触发的事件
end


function IsInKongFuMasterFuben( fubenId )
	return KongFuMasterConfig[1].fubenId == fubenId
end

function GetKongFuMasterCfgByIdx( masterIdx )
	return KongFuMasterConfig[masterIdx]
end

--[[
掉线恢复的一些配置
]]
function GetElemsForKongFuMasterBoss( fubenPtr )
	local masterIdx = Fuben.getFbValue( fubenPtr, KONGFUMASTER_FUBEN_MASTERIDX )
	local masterCfg = GetKongFuMasterCfgByIdx( masterIdx )
	if not masterCfg then
		return
	end

	local fubenCfg 				= masterCfg
	local InitFubenRightPanel 	= InitKongFuMasterPanel
	local loginLog   			= "kongfu login" 
	local loginLogOk			= "kongfu login ok" 
	return fubenCfg, InitFubenRightPanel, loginLog, loginLogOk
end



function EnterKongFuMaster( sysarg, masterIdx )
	local masterCfg = KongFuMasterConfig[masterIdx]
	if not masterCfg then
		return
	end

	if CommonEnterScence( sysarg, masterCfg, 0, "", KongFuMasterCheckEnter, KongFuMasterBeforeEnter ) then
		local fubenPtr = Actor.getFubenPrt( sysarg )
		Fuben.setFbValue( fubenPtr, KONGFUMASTER_FUBEN_ALLHURT, 0 )
		Fuben.setFbValue( fubenPtr, KONGFUMASTER_FUBEN_ATTACKNUM, 0 )
		Fuben.setFbValue( fubenPtr, KONGFUMASTER_FUBEN_MASTERIDX, masterIdx )
		Fuben.setFbValue( fubenPtr, KONGFUMASTER_FUBEN_SCENEID, masterCfg.sceneId )

		InitKongFuMasterPanel( sysarg, masterCfg )
	end
end

function KongFuMasterCheckEnter( sysarg, sceneCfg )
	--检查进入次数
	if not CheckActorDailyDoneTimes( sysarg, enDailyOpTimes_KongFuMaster, sceneCfg.masterIdx ) then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.COM0021, ttFlyTip)
		return false
	end
	return true
end

function KongFuMasterBeforeEnter( sysarg, sceneCfg )
	--正式副本要增加进入次数
	AddActorDailyDoneTimes( sysarg, enDailyOpTimes_KongFuMaster, sceneCfg.masterIdx, 1 )		--进入次数+1
	return true
end

function InitKongFuMasterPanel( sysarg, masterCfg )
	local fubenPtr 	= Actor.getFubenPrt( sysarg )
	local allHurt  	= Fuben.getFbValue( fubenPtr, KONGFUMASTER_FUBEN_ALLHURT ) or 0
	local attackNum = Fuben.getFbValue( fubenPtr, KONGFUMASTER_FUBEN_ATTACKNUM ) or 0
	
	local contStyle 	= 7
	local buttons 		= {1}
	local activityId 	= enRightPanelActivity_KongFuMaster
	local title			= Lang.ScriptTips.KongFuMasterName
	local cfg 		= {}
	cfg.allHurt 	= allHurt
	cfg.attackNum 	= attackNum
	cfg.awards		= masterCfg.showAwards
	OpenCommonStylePanel( sysarg, 1, activityId, 0, title, cfg, contStyle, buttons  )
end

function ChangeKongFuMasterPanel( sysarg )
	local fubenPtr 	= Actor.getFubenPrt( sysarg )
	local allHurt  	= Fuben.getFbValue( fubenPtr, KONGFUMASTER_FUBEN_ALLHURT ) or 0
	local attackNum = Fuben.getFbValue( fubenPtr, KONGFUMASTER_FUBEN_ATTACKNUM ) or 0

	local activitySts 	= 1
	local contStyle 	= 7
	local buttons1 		= {1}
	local buttons2 		= {1}
	local cfg 			= {}
	cfg.allHurt 		= allHurt
	cfg.attackNum 		= attackNum
	ChangeCommonStylePanel( sysarg, activitySts, contStyle, cfg, buttons1, buttons2 )
end


function OnKongFuMasterHurted( sysarg, bossPtr, hurtValue )
	local fubenPtr 	= Actor.getFubenPrt( sysarg )
	local allHurt  	= Fuben.getFbValue( fubenPtr, KONGFUMASTER_FUBEN_ALLHURT ) or 0
	local attackNum = Fuben.getFbValue( fubenPtr, KONGFUMASTER_FUBEN_ATTACKNUM ) or 0
	allHurt 	= allHurt + hurtValue
	attackNum 	= attackNum + 1
	Fuben.setFbValue( fubenPtr, KONGFUMASTER_FUBEN_ALLHURT, allHurt )
	Fuben.setFbValue( fubenPtr, KONGFUMASTER_FUBEN_ATTACKNUM, attackNum )

	--print("OnKongFuMasterHurted, hurtValue="..hurtValue..", allHurt="..allHurt..", attackNum="..attackNum)
	ChangeKongFuMasterPanel( sysarg )
end

--[[
--sysarg:归属玩家
--lastHitKiller：最后一击玩家
注意：怪物死亡时，玩家可能不在线，要确保副本进度能够继续进行
]]
function OnKongFuMasterDie( monster, sysarg, monId, lastHitKiller )
	local fubenPtr = Actor.getFubenPrt( monster )
	OnKongFuMasterOver( sysarg, fubenPtr, 1 )
end


--[[游戏通关
sts:0-失败，1-成功
注意：sysarg此时有可能不在副本中
]]
function OnKongFuMasterOver( sysarg, fubenPtr, sts )
	local masterIdx = Fuben.getFbValue( fubenPtr, KONGFUMASTER_FUBEN_MASTERIDX )
	--print("OnKongFuMasterOver, masterIdx="..masterIdx)
	local masterCfg = GetKongFuMasterCfgByIdx( masterIdx )
	if not masterCfg then
		return
	end

	local panelInfo = {}
	panelInfo.subActivityId = masterCfg.masterIdx
	panelInfo.sts 			= sts 			--这里是通关面板的状态
	if sts == 1 then
		panelInfo.awards = masterCfg.showAwards
	else
		local masterId 		=  masterCfg.monsters[1].monsterId
		--print("OnKongFuMasterOver, masterId="..masterId)
		--local sceneHandle 	=  Actor.getSceneHandle( sysarg )
		local sceneId 		= Fuben.getFbValue( fubenPtr, KONGFUMASTER_FUBEN_SCENEID )
		local fubenHandle	= Fuben.getFubenHandle( fubenPtr )
		local sceneHandle 	= Fuben.getSceneHandleById( sceneId, fubenHandle )
		local master 		= Fuben.getMonsterPtrById( sceneHandle, masterId )
		local lastRate		= 0
		if master then
			local masterHpMax 	=  Actor.getIntProperty(master, PROP_CREATURE_MAXHP)
			local masterHp 		=  Actor.getIntProperty(master, PROP_CREATURE_HP)
			lastRate			=  math.ceil( masterHp * 100 / masterHpMax )
			--print("OnKongFuMasterOver, masterHpMax="..masterHpMax..", masterHp="..masterHp..", lastRate="..lastRate)
		end
		panelInfo.param1 = lastRate
	end
	
	PassActivityRightPanel( sysarg, 1, enRightPanelActivity_KongFuMaster, panelInfo )
	Actor.exitFuben( sysarg )
end

ResisterKongFuMasterProc()
